<?php

/**
 * Created by PhpStorm.
 * User: wen
 * Date: 2017/6/21
 * Time: 9:43
 */
namespace V1;
use Util\LogUtil;

class Order extends \ResourceClass
{
    public $isActive = true;
    public $remark = '订单接口';

    function __construct()
    {
        require_once(ROOT . 'lib/AliPay/AliPayConfig.php');
        require_once(ROOT . 'lib/WxPay/WxPay.php');
    }
    public function get(array $req, array $post, &$result)
    {

        return '';
    }
    public function post(array $req, array $post, &$result)
    {
        if (!isset($req['uid'])) {
            return 'require UID';
        }
        if (!array_key_exists('payload', $post)) {
            return 'require payload';
        }
        $data = base64_decode($post['payload']);
        @parse_str($data, $data);
        if(!isset($data['article_id'])) {
            return '文章id 不能为空';
        }

        $products = json_decode($data['product_list']);
        if (empty($products)) {
            return '至少选择一个商品';
        }
        try{
            $order_biz = new \Biz\Order();
            $article_cls = new \Orm\Article\ArticleClass();
            $article_cls->loadById($data['article_id']);
            $data['article_id'] = $data['article_id'];
            $data['seller_uid'] = $article_cls['uid'];
            $data['buyer_uid'] = $req['uid'];
            $order = $order_biz->createOrder($data,$products);
            $result = array(
              'order_id'=>$order
            );
        }catch (\Exception $e){
            return $e->getMessage();
        }
        return '';
    }
    public function put(array $req, array $post, &$result)
    {
        if (!isset($req['id'])) {
            return 'require resource id';
        }
        if (!isset($req['uid'])) {
            return 'require UID';
        }
        if (!array_key_exists('payload', $post)) {
            return 'require payload';
        }
        $data = base64_decode($post['payload']);
        @parse_str($data, $data);
        switch ($data['actionType']) {
            case "submit":/*生成订单流水号*/
                if (!array_key_exists('contact', $data)) {
                    return 'essential data required';
                }
                if (!array_key_exists('tel', $data)) {
                    return 'essential data required';
                }
                if (!array_key_exists('appointment_time', $data)) {
                    return 'essential data required';
                }
                if (!array_key_exists('adds', $data)) {
                    return 'essential data required';
                }
                $result='';
                $transaction = new \Biz\Transaction();
                $transaction_res = $transaction->createTransaction($req,$data);
                if ($transaction_res) {
                    $result = $transaction_res;
                }
                break;
            case "prePay":/*发起支付*/
                if (!isset($data['open_id']) && empty($data['open_id'])) {
                    return '缺少open_id';
                }
                $order_biz = new \Biz\Order();
                /*查询订单状态 是否是未支付订单*/
                $order_status = $order_biz->getOrderStatusById($req['id']);
                if (!$order_status) {
                    return '未查询到有效的订单';
                }
                /*用orderID查询订单信息*/
                $order_info = $order_biz ->getOrderInfoById($req['id']);
                if ($data['platform'] == 'WX') {
                    $result = self::Wxpay($order_info,$data['open_id']);
                    LogUtil::log('$result', json_encode($result));
                }
                break;
            default:
                return 'unkown options';
        }
    }
    //='o5GoWs7wwOz1-vc0oQXtlo1W8jgY'
    private function Wxpay($order_info, $open_id)
    {
        $order_data = $order_info['order_info'];
        $outTradeNo = $order_info['order_transaction']['out_trade_no'];
        $wxPayConfig = \WxPay::getPayConfig();
        $checkOrder = \WxPay::checkOrderQuery($wxPayConfig, $outTradeNo);
        $orderData['ordercode'] = $order_data['order_code'];
        $orderData['need_pay_price'] = $order_data['total_price'];
        $orderData['id'] = $order_data['id'];
        $orderData['insert_time'] = $order_data['insert_time'];
        if ($checkOrder == 'CHECK_FAILED') { /*微信算签名*/
            //获取微信配置
            $WxPaysign = \WxPay::getPreSign($wxPayConfig, $orderData, $outTradeNo, $open_id);
            $msgData['result'] = 'OK';
            $msgData['data'] = array('rsa' => $WxPaysign, 'out_trade_no' => $outTradeNo);//返回微信支付签名，流水号
            return $msgData;
        }
        $msgData['msg'] = 'AGAIN_PRE_PAY';
        return $msgData;
    }
}